﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_AddDepartment : System.Web.UI.Page
{
    CommonMethods cMethod = new CommonMethods();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindCompanies();
            BindBranches();
            BindDepartments();
        }
    }
    private void SaveDepartment()
    {
        if (btnSaveDepartment.Text == "Save")
        {

            using (POSEntities db = new POSEntities())
            {
                Department department = new Department();
                department.Dept_Name = txtDepartmentName.Text;
                department.BranchId = Convert.ToInt32(DDL_Branch.SelectedValue);
                department.Is_Deleted = false;
                db.Departments.Add(department);
                db.SaveChanges();
            }
        }
        else if (btnSaveDepartment.Text == "Update")
        {
            using (POSEntities db = new POSEntities())
            {
                Int32 id = Convert.ToInt32(btnSaveDepartment.CommandArgument);
                var department = (from b in db.Departments where (b.Dept_ID == id) select b).FirstOrDefault();
                department.Dept_Name = txtDepartmentName.Text;
                db.SaveChanges();
            }
        }
        BindDepartments();
        clearForm();
    
}
    private void BindDepartments()
    {
        if (DDL_Branch.Items.Count>0)
        {
           GridViewDepartments.DataSource= cMethod.getDepartmentWithBranchId(Convert.ToInt32(DDL_Branch.SelectedValue));
            GridViewDepartments.DataBind();
        }
    }
    private void BindBranches()
    {
        if (DDL_Company.Items.Count > 0)
        {
            DDL_Branch.DataSource=cMethod.getBranchesWithCompanyId(Convert.ToInt32(DDL_Company.SelectedValue));
            DDL_Branch.DataBind();
        }
    }
    private void BindCompanies()
    {

            DDL_Company.DataSource= cMethod.getAllCompanies();
            DDL_Company.DataBind();

    }
    protected void DDL_Company_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindBranches();
    }

    protected void DDL_Branch_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindDepartments();
    }

    protected void btnSaveDepartment_Click(object sender, EventArgs e)
    {
        SaveDepartment();
    }

    protected void lbtnEditDepartment_Click(object sender, EventArgs e)
    {
        LinkButton lbtn = (LinkButton)sender;
        int cId = Convert.ToInt32(lbtn.CommandArgument);
        using (POSEntities db = new POSEntities())
        {
            var department = (from d in db.Departments
                               join b in db.Branches on d.BranchId equals b.BranchId
                               join c in db.Companies on b.CompanyId equals c.CompanyId
                               where (d.Dept_ID==cId) && d.Is_Deleted == false
                               select new { c.CompanyId, c.CompanyName, b.BranchId, b.BranchName, d.Dept_ID, d.Dept_Name }).FirstOrDefault();
            DDL_Company.SelectedValue = department.CompanyId.ToString();
            BindBranches();
            DDL_Branch.SelectedValue = department.BranchId.ToString();
            txtDepartmentName.Text = department.Dept_Name;
            btnSaveDepartment.CommandArgument = department.Dept_ID.ToString();
            btnSaveDepartment.Text = "Update";
        }
    }
    private void clearForm()
    {
        txtDepartmentName.Text = "";
        btnSaveDepartment.Text = "Save";
        btnSaveDepartment.CommandArgument = "";
    }
    protected void lbtnDeleteBranch_Click(object sender, EventArgs e)
    {
        LinkButton lbtn = (LinkButton)sender;
        int cId = Convert.ToInt32(lbtn.CommandArgument);
        using (POSEntities db = new POSEntities())
        {
            var department = (from c in db.Departments
                          where c.Dept_ID == cId
                          select c).FirstOrDefault();
            department.Is_Deleted = true;
            db.SaveChanges();
            BindDepartments();
        }
    }

    protected void btnClearForm_Click(object sender, EventArgs e)
    {
        clearForm();
    }
    [WebMethod]
    public static void SaveDepartment(Department department)
    {
        using (POSEntities db = new POSEntities())
        {
            department.BranchId = 1;
            db.Departments.Add(department);
            db.SaveChanges();
        }
    }
}